<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
class Bag
{
	private $CI;

	public function __construct()
	{
		$this->CI = &get_instance();
	}

	public function encrypt($password)
	{
		return password_hash($password, PASSWORD_DEFAULT);
	}

	public function verify($password, $hash)
	{
		return password_verify($password, $hash);
	}

	public function send_email($email, $captcha, $from, $smtpserver, $smtpport, $smtpuser, $smtppass)
	{
		$this->CI->load->library('mail');
		$subject = '验证您的邮箱---' . $this->sys['name'] . '';
		$body = '<p>尊敬的' . $this->sys['name'] . "用户，您好:</p>\r\n\t\t\t\t<p>您在 " . date('Y-m-d H:i:s', time()) . " 提交了验证邮箱请求，请使用以下</p>\r\n\t\t\t\t<p style=\"color:red;\">验证码：<b>" . $captcha . "</b></p>\r\n\t\t\t\t<p>您若未提交此请求，您可以安全的忽略此邮件。可能有人错误的输入了您的邮件地址。</p>\r\n\t\t\t\t<p>感谢您的支持 ! " . $this->sys['name'] . '服务团队</p>';
		$smtpserver = $smtpserver;
		$smtpserverport = $smtpport;
		$smtpusermail = $from;
		$smtpemailto = $email;
		$smtpuser = $smtpuser;
		$smtppass = $smtppass;
		$mailsubject = $subject;
		$mailsubject = '=?UTF-8?B?' . base64_encode($mailsubject) . '?=';
		$mailbody = $body;
		$mailtype = 'HTML';
		$this->mail->smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
		$this->mail->debug = false;
		$this->mail->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
	}

	public function get_address($host, $username, $password, $port, $by)
	{
		require_once $_SERVER['DOCUMENT_ROOT'] . '/../Bitcoin/bitcoin.inc';
		$btc = new BitcoinClient('http', $username, $password, $host, $port);
		return $btc->getnewaddress($by);
	}

	public function get_transactions($host, $username, $password, $port, $by)
	{
		require_once $_SERVER['DOCUMENT_ROOT'] . '/../Bitcoin/bitcoin.inc';
		$btc = new BitcoinClient('http', $username, $password, $host, $port);
		return $btc->listtransactions($by);
	}

	public function get_ethaddress($host, $port, $by)
	{
		$data = array(
			'jsonrpc' => '2.0',
			'method'  => 'personal_newAccount',
			'params'  => array($by),
			'id'      => 1
			);
		$data_string = json_encode($data);
		$ch = curl_init('http://' . $host . ':' . $port);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
		$result = curl_exec($ch);
		$arr = json_decode($result, true);
		return $arr['result'];
	}

	public function get_accounts($host, $port)
	{
		$data = array('jsonrpc' => '2.0', 'method' => 'eth_accounts', 'params' => '[]', 'id' => 1);
		$data_string = json_encode($data);
		$ch = curl_init('http://' . $host . ':' . $port);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
		$result = curl_exec($ch);
		$result = json_decode($result, true);
		return $result['result'];
	}

	public function get_balance($host, $port, $address)
	{
		$data = array(
			'jsonrpc' => '2.0',
			'method'  => 'eth_getBalance',
			'params'  => array($address, 'latest'),
			'id'      => 1
			);
		$data_string = json_encode($data);
		$ch = curl_init('http://' . $host . ':' . $port);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
		$result = curl_exec($ch);
		$result = json_decode($result, true);
		return hexdec($result['result']) / 1.0E+18;
	}

	public function captcha()
	{
		if (isset($this->session->img_captcha['filename']) && $this->session->img_captcha['filename']) {
			unlink(FCPATH . 'captcha/' . $this->session->img_captcha['filename']);
		}

		if (!is_dir($_SERVER['DOCUMENT_ROOT'] . '/captcha/')) {
			mkdir($_SERVER['DOCUMENT_ROOT'] . '/captcha/');
		}

		$vals = array(
			'img_path'    => './captcha/',
			'img_url'     => base_url('/captcha/'),
			'font_path'   => './fonts/simhei.ttf',
			'img_width'   => '100',
			'img_height'  => 39,
			'expiration'  => 7200,
			'word_length' => 4,
			'font_size'   => 20,
			'img_id'      => 'Imageid',
			'pool'        => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
			'colors'      => array(
				'background' => array(255, 255, 255),
				'border'     => array(255, 255, 255),
				'text'       => array(0, 0, 0),
				'grid'       => array(255, 40, 40)
				)
			);
		$cap = create_captcha($vals);
		$this->session->img_captcha = $cap;
		echo $cap['image'];
	}
}

defined('BASEPATH') || exit('No direct script access allowed');

?>
